<template>
  <div class="consult-list">
    <van-list
      v-model:loading="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <consult-item @on-delete="onDelete" v-for="item in list" :key="item.id" :item="item" />
    </van-list>
  </div>
</template>

<script setup lang="ts">
import { ConsultType } from '@/enums'
import { getConsultOrderList } from '@/api/consult'
import { ConsultOrderItem } from '@/types/consult'
import { ref } from 'vue'
import ConsultItem from './ConsultItem.vue'

const props = defineProps<{
  type: ConsultType
}>()

const list = ref<ConsultOrderItem[]>([])
const params = {
  type: props.type,
  current: 1,
  pageSize: 10,
}
const loading = ref(false)
const finished = ref(false)

const onLoad = async () => {
  // 异步更新数据
  // setTimeout 仅做示例，真实场景中一般为 ajax 请求
  const { data } = await getConsultOrderList(params)
  list.value.push(...data.rows)

  // 加载状态结束
  loading.value = false

  // 数据全部加载完成
  if (list.value.length === data.total) {
    finished.value = true
  } else {
    params.current++
  }
}

const onDelete = (id: string) => {
  list.value = list.value.filter((item: any) => item.id !== id)
}
</script>

<style lang="scss" scoped>
.consult-list {
  padding: 10px 15px;
}
</style>
